/*
 * Eugen Stoian <stoian.e@gmail.com> 2010
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "VariableNode.h"
#include "ValueNode.h"

VariableNode::VariableNode(const QString& info, int pos, const Real* var):_variable(var),_info(info),_pos(pos) {
}
/**
 *\fn SmartPtr<ExpressionTreeNode> VariableNode::copy() const
 *\brief Returns a deep copy of the tree having as root the current node
 * Reimplemented in derived classes
 *
 */
ExpressionTreeNode* VariableNode::copy() const{
    return new VariableNode(_info,_pos,_variable);
}


/**
 *\fn Real VariableNode::eval() const
 *\brief Recursively evaluates contained math expression
 * Reimplemented in derived classes
 *
 */
Real VariableNode::eval() const{
    if(_variable)
        return *_variable;
    return 0.0;
}
/**
 *\fn SmartPtr<ExpressionTreeNode> VariableNode::derive(const QString& varName) const
 *\brief Returns the tree associated to the symbolic math derivative of the contained math expression
 *\param varName Name of the variable with respect to which the derivative is computed
 * Reimplemented in derived classes
 */
SmartPtr<ExpressionTreeNode> VariableNode::derive(const QString& varName) const{
    if(varName == _info)
        return new ValueNode(1.0);
    return new ValueNode(0.0);
}

/**
 *\fn SmartPtr<ExpressionTreeNode> VariableNode::firstLevelSimplification() const
 *\brief
 * Reimplemented in derived classes
 */
SmartPtr<ExpressionTreeNode> VariableNode::firstLevelSimplification() const {
    return copy();
}

/**
 *\fn QString VariableNode::toPrefixString() const
 *\brief
 * Reimplemented in derived classes
 */
QString VariableNode::toPrefixString() const {
    return _info;
}

/**
 *\fn QString VariableNode::toPostfixString() const
 *\brief
 * Reimplemented in derived classes
 */
QString VariableNode::toPostfixString() const {
    return _info;
}

/**
 *\fn QString VariableNode::toInfixString() const
 *\brief
 * Reimplemented in derived classes
 */
QString VariableNode::toInfixString() const {
    return _info;
}
/**
 *\fn EpressionTreeNode* VariableNode::replace(const QString& varName, ExpressionTreeNode* tree) const
 *\brief
 *
 */
ExpressionTreeNode* VariableNode::replace(const QString& varName, ExpressionTreeNode* tree) const {
    if(varName == _info)
        return tree->copy();
    else
        return copy();
}


/**
 *\fn EpressionTreeNode* VariableNode::replace(int varPos, ExpressionTreeNode* tree) const
 *\brief
 *
 */
ExpressionTreeNode* VariableNode::replace(int varPos, ExpressionTreeNode* tree) const {
    if(varPos == _pos)
        return tree->copy();
    else
        return copy();
}
